package com.one.shopping.ui.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTabHost;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.one.shopping.R;
import com.one.shopping.ui.fragment.OrdersItemFragment;
import com.one.shopping.ui.fragment.ShopGoodsFragment;
import com.one.shopping.ui.fragment.UserDetailFragment;

/**
 * @author 柯尊诚
 * @version 1.0
 * @date 创建时间：2016/1/8 17:54
 */
public class TabHostActivity extends FragmentActivity{

    private FragmentTabHost tabHost;
    private RadioGroup radioGroup;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_tabhost);
        radioGroup = (RadioGroup) findViewById(R.id.radio_group);
        tabHost = (FragmentTabHost) findViewById(R.id.tab_host);
        tabHost.setup(this, getSupportFragmentManager(), R.id.frame_layout);

        tabHost.addTab(tabHost.newTabSpec("tab1").setIndicator("tab1"), ShopGoodsFragment.class, null);
        tabHost.addTab(tabHost.newTabSpec("tab2").setIndicator("tab2"), OrdersItemFragment.class, null);
        tabHost.addTab(tabHost.newTabSpec("tab3").setIndicator("tab3"), UserDetailFragment.class, null);

        tabHost.getTabWidget().setVisibility(View.GONE);
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId) {
                    case R.id.rb_tab_1:
                        tabHost.setCurrentTabByTag("tab1");
                        radioGroup.check(R.id.rb_tab_1);
                        break;
                    case R.id.rb_tab_2:
                        tabHost.setCurrentTabByTag("tab2");
                        break;
                    case R.id.rb_tab_3:
                        tabHost.setCurrentTabByTag("tab3");
                        break;
                    default:
                        break;
                }
            }
        });
    }

    public void setTitle(String title){
        TextView titleValues = (TextView) findViewById(R.id.main_title_tv);
        if (titleValues != null) {
            titleValues.setText(title);
        }
    }

    /**
     * 是否显示查询按钮
     */
    public void isVisibleQuery(boolean tag) {
        ImageView queryBtn = (ImageView) findViewById(R.id.main_query_btn_iv);
        if (queryBtn != null) {
            if (tag)
                queryBtn.setVisibility(View.VISIBLE);
            else
                queryBtn.setVisibility(View.GONE);
        }
    }

    /**
     * 是否显示标题
     */
    public void isVisibleTitle(boolean tag) {
        TextView titleValue = (TextView) findViewById(R.id.main_title_tv);
        if (titleValue != null) {
            if (tag)
                titleValue.setVisibility(View.VISIBLE);
            else
                titleValue.setVisibility(View.GONE);
        }
    }

    /**
     * 是否显示扫描按钮和输入框
     */
    public void isVisibleScanFind(boolean tag) {
        ImageView scanBtn = (ImageView) findViewById(R.id.main_scan_btn_iv);
        EditText queryField = (EditText) findViewById(R.id.main_query_values_et);
        if (scanBtn != null && queryField != null) {
            if (tag) {
                scanBtn.setVisibility(View.VISIBLE);
                queryField.setVisibility(View.VISIBLE);
            } else {
                scanBtn.setVisibility(View.GONE);
                queryField.setVisibility(View.GONE);
            }
        }
    }

}
